unit AccountEditFrm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, cxGraphics, cxControls, cxLookAndFeels,
  cxLookAndFeelPainters, dxSkinsCore, dxSkinOffice2013White, dxSkinscxPCPainter,
  cxContainer, cxEdit, dxLayoutcxEditAdapters, dxLayoutControlAdapters,
  Vcl.ComCtrls, dxCore, cxDateUtils, Vcl.Menus, Vcl.StdCtrls, cxButtons,
  dxLayoutLookAndFeels, cxDBExtLookupComboBox, cxDropDownEdit, cxLookupEdit,
  cxDBLookupEdit, cxDBLookupComboBox, cxMaskEdit, cxCalendar, dxLayoutContainer,
  cxTextEdit, dxLayoutControl, Data.DB, FIBDataSet, pFIBDataSet;

type
  TAccountEditForm = class(TForm)
    grp1: TdxLayoutGroup;
    lc1: TdxLayoutControl;
    txt_SNOMER: TcxTextEdit;
    itmlc1Item1: TdxLayoutItem;
    dt_SDATA: TcxDateEdit;
    itmlc1Item11: TdxLayoutItem;
    cbb_KONTRAGENT: TcxLookupComboBox;
    itmlc1Item12: TdxLayoutItem;
    cbb_DOG: TcxExtLookupComboBox;
    itmlc1Item13: TdxLayoutItem;
    txt_NOTE: TcxTextEdit;
    itmlc1Item14: TdxLayoutItem;
    grplc1Group1: TdxLayoutGroup;
    grplc1Group2: TdxLayoutGroup;
    grplc1Group4: TdxLayoutGroup;
    dxlytlkndflst1: TdxLayoutLookAndFeelList;
    dxlytcxlkndfl1: TdxLayoutCxLookAndFeel;
    btnCancel: TcxButton;
    itmlc1Item15: TdxLayoutItem;
    btnSave: TcxButton;
    itmlc1Item16: TdxLayoutItem;
    grplc1Group3: TdxLayoutGroup;
    procedure btnCancelClick(Sender: TObject);
    procedure cbb_KONTRAGENTPropertiesEditValueChanged(Sender: TObject);
    procedure txt_SNOMERPropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
    procedure dt_SDATAPropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
    procedure cbb_KONTRAGENTPropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
    procedure cbb_DOGPropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
    procedure btnSaveClick(Sender: TObject);
  private
    { Private declarations }
    DS: TpFIBDataSet;
    isNewRec: Boolean;
    procedure PrepareDS;
    procedure FillControlls;
    procedure ReopenDog;
    procedure AddRec(isNew: Boolean);
    function Validate: Boolean;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent; dsOut: TpFIBDataSet; isNew: Boolean = false);
  end;

var
  AccountEditForm: TAccountEditForm;

implementation

{$R *.dfm}

uses cmDBUnit;

procedure TAccountEditForm.AddRec(isNew: Boolean);
begin
  if not Validate then Exit;
  with DS do begin
    if isNew then Append else Edit;
      FN('SNOMER').AsVariant:= txt_SNOMER.EditValue;
      FN('SDATA').AsVariant:= dt_SDATA.EditValue;
      FN('KONTRAGENT').AsVariant:= cbb_KONTRAGENT.EditValue;
      FN('DOG').AsVariant:= cbb_DOG.EditValue;
    Post;
  end;
end;

procedure TAccountEditForm.btnCancelClick(Sender: TObject);
begin
  ModalResult:= mrCancel;
end;

procedure TAccountEditForm.btnSaveClick(Sender: TObject);
begin
  AddRec(isNewRec);
  ModalResult:= mrOk;
end;

procedure TAccountEditForm.cbb_DOGPropertiesValidate(Sender: TObject;
  var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
begin
  Error:= cbb_DOG.EditValue = null;
end;

procedure TAccountEditForm.cbb_KONTRAGENTPropertiesEditValueChanged(
  Sender: TObject);
begin
  ReopenDog;
end;

procedure TAccountEditForm.cbb_KONTRAGENTPropertiesValidate(Sender: TObject;
  var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
begin
  Error:= cbb_KONTRAGENT.EditValue = null;
end;

constructor TAccountEditForm.Create(AOwner: TComponent; dsOut: TpFIBDataSet;
  isNew: Boolean);
begin
  inherited Create(AOwner);
  PrepareDS;
  DS:= dsOut;
  isNewRec:= isNew;
  if not isNew then FillControlls;
end;

procedure TAccountEditForm.dt_SDATAPropertiesValidate(Sender: TObject;
  var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
begin
  Error:= DisplayValue = null;
end;

procedure TAccountEditForm.FillControlls;
begin
  with DS do begin
    txt_SNOMER.EditValue:= FN('SNOMER').AsVariant;
    dt_SDATA.EditValue:= FN('SDATA').AsVariant;
    cbb_KONTRAGENT.EditValue:= FN('KONTRAGENT').AsVariant;
    cbb_DOG.EditValue:= FN('DOG').AsVariant;
    txt_NOTE.EditValue:= FN('NOTE').AsVariant;
  end;
end;

procedure TAccountEditForm.PrepareDS;
begin
  cmDb.ds_KONTRAGENT.Open;

end;

procedure TAccountEditForm.ReopenDog;
begin
  with cmDb.ds_DOG do begin
    Close;
    MainWhereClause:='D.KONTRAGENT = :kontr';
    ParamByName('kontr').AsVariant:= cbb_KONTRAGENT.EditValue;
    Open;
  end;
end;

procedure TAccountEditForm.txt_SNOMERPropertiesValidate(Sender: TObject;
  var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
begin
  Error:= VarToStr(Trim(DisplayValue)) = '';
end;

function TAccountEditForm.Validate: Boolean;
begin
  Result:= False;
  if not cbb_KONTRAGENT.ValidateEdit(False) then Exit;
  if not txt_SNOMER.ValidateEdit(false) then Exit;
  if not dt_SDATA.ValidateEdit(false) then Exit;
  if not cbb_DOG.ValidateEdit(false) then Exit;
  Result:= True;
end;

end.
